package com.web.service.Impl;

import com.web.constant.UserConstants;
import com.web.dao.AddressDao;
import com.web.dao.UserAddressDao;
import com.web.exception.BusinessException;
import com.web.pojo.domain.Address;
import com.web.service.AddressServer;
import com.web.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: wws
 * @Date: 2021/1/25 17:24
 * describe:
 */
@Service
public class AddressServerImpl implements AddressServer {

    @Autowired
    AddressDao addressDao;

    @Autowired
    UserAddressDao userAddressDao;

    /**
     * 新增地址信息
     * @param address 地址信息
     * @return 结果
     */
    @Override
    public Integer insertAddress(Address address) {
        Integer userId = SecurityUtils.getUserId();
        if (address.getPid() == 0) {
            address.setPids("0");
            addressDao.insertAddress(address);
            return userAddressDao.insertUserAddress(userId, address.getAddressId());
        } else {
            Address parent = addressDao.selectAddressById(address.getPid());
            // 如果父节点不为正常状态,则不允许新增子节点
            if (UserConstants.ADDRESS_DISABLE.equals(parent.getDelFlag())) {
                throw new BusinessException("地址停用，不允许新增");
            }
            address.setPids(parent.getPids() + "," + address.getPid());
            addressDao.insertAddress(address);
            return userAddressDao.insertUserAddress(userId, address.getAddressId());
        }
    }

    /**
     * 修改地址信息
     * @param address 地址信息
     * @return 结果
     */
    @Override
    public Integer updateAddress(Address address) {
        if (address.getPid() == 0) {
            address.setPids("0");
        } else {
            Address parent = addressDao.selectAddressById(address.getPid());
            // 如果父节点不为正常状态,则不允许新增子节点
            if (UserConstants.ADDRESS_DISABLE.equals(parent.getDelFlag())) {
                throw new BusinessException("地址停用，不允许新增");
            }
            address.setPids(parent.getPids() + "," + address.getPid());
        }

        return addressDao.updateAddress(address);
    }

    /**
     * 删除设备树信息
     * @param addressId 地址id
     * @return 结果
     */
    @Override
    public Integer deleteAddress(String updateBy, Integer addressId) {
        return addressDao.deleteAddress(updateBy, addressId);
    }

    /**
     * 检查地址名称是否唯一
     * @param address 地址信息
     * @return 结果
     */
    @Override
    public Boolean checkAddressNameUnique(Address address) {
        Integer addressId = address.getAddressId() == null ? -1 : address.getAddressId();
        if (address.getPid() == 0) {
            Integer userId = SecurityUtils.getUserId();
            Address checkAddress = addressDao.checkAddressRootNodeNameUnique(address.getPid(), address.getAddressName(), userId);
            if (checkAddress != null && !addressId.equals(checkAddress.getAddressId())) {
                return false;
            }
        } else {
            Address checkAddress = addressDao.checkAddressNameUnique(address.getPid(), address.getAddressName());
            if (checkAddress != null && !addressId.equals(checkAddress.getAddressId())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 查询地址树列表
     * @param userId 用户Id
     * @param addressName 地址名
     * @return 结果
     */
    @Override
    public List<Address> selectAddressList(Integer userId, String addressName) {
        return addressDao.selectAddressList(userId,addressName);
    }


    /**
     * 查询地址下所拥有的网关
     * @param addressId 地址id
     * @return 结果
     */
    @Override
    public Boolean checkAddressExistGateWay(Integer addressId) {
        return addressDao.checkAddressExistGateWay(addressId) > 0 ? true : false;
    }

    /**
     * 查询地址下所拥有的设备
     * @param addressId 地址id
     * @return 结果
     */
    @Override
    public Boolean checkAddressExistDevice(Integer addressId) {
        return addressDao.checkAddressExistDevice(addressId) > 0 ? true : false;
    }

    /**
     * 查询地址树节点下是否拥有子节点
     * @param addressId 地址id
     * @return 结果
     */
    @Override
    public Boolean checkAddressExistChild(Integer addressId) {
        return addressDao.checkAddressExistChild(addressId) > 0 ? true : false;
    }


    /**
     * 查询地址树信息
     * @param addressId 地址id
     * @return 结果
     */
    @Override
    public Address selectAddress(Integer addressId) {
        return addressDao.selectAddress(addressId);
    }

    /**
     * 查看地址根节点
     * @param userId 用户Id
     * @return 结果
     */
    @Override
    public List<Address> selectAddressRootNode(Integer userId) {
        return userAddressDao.selectAddressByUserId(userId);
    }

    /**
     * 查看地址子节点列表
     * @param addressId 地址id
     * @return 结果
     */
    @Override
    public List<Address> selectAddressChildNodeList(Integer addressId) {
        return addressDao.selectAddressChildNodeList(addressId);
    }

    /**
     * 地址授权
     * @param ids 授权的Id
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public Integer authorizeAddress(List<Integer> ids, Integer userId) {
        return addressDao.authorizeAddress(ids,userId);
    }
}
